<?php 

class admin_recursoModel extends Object{
    
    private $hide = array('.DS_Store');
    private $doc = array();
    public function ListarRecursos(){
        $out = array();
        $this->LoadResource("files/dir", "dir");
        $files = $this->dir->getArquivos(RESOURCES);
        foreach($files as $name){
            if(in_array($name, $this->hide)) continue;
            $out[] = $name;
        }
        return $out;
    }
    
    public function menuRecursos(){
        $out = array();
        $this->LoadResource("files/dir", "dir");
        $files = $this->dir->getArquivos(RESOURCES);
        foreach($files as $name){
            if(in_array($name, $this->hide)) continue;
            $out[$name] = 'admin/recurso/show/'.$name;
        }
        return $out;
    }
    
    public function getResources($recurso){
        $this->LoadResource("files/dir", "dir");
        $file_name = RESOURCES . $recurso;
        $files = $this->dir->getArquivos($file_name);
        $out = array();
        foreach($files as $file)
            //if(is_file($file_name . "/$file")) continue;
            if(strpos($file, "Resource.php") !== false){
                $name = str_replace("Resource.php", "", $file);
                $out[$name] = "$recurso/$name";
            }
        return $out;
    }
    
    public function getJsplugins($recurso){
        $this->LoadResource("files/dir", "dir");
        $file_name = RESOURCES . $recurso . "/jsplugins/";
        //if(!file_exists($file)) return array();
        
        $files = $this->dir->getArquivos($file_name);
        $out = array();
        foreach($files as $file){
            if(in_array($file, $this->hide)) continue;
            if(is_file($file_name . "/$file")) continue;
            $out[$file] = "$recurso/$file";
        }
        return $out;
    }
    
    public function getRecVar($recurso, $subrecurso, $var){
        if(!$this->loadDoc($recurso, $subrecurso)){
            return "A classe ($subrecurso"."Doc) não existe";
        }
        $obj    = $this->doc[$subrecurso . "Doc"];
        $method = "get$var";
        return $obj->$method();
    }
    
    public function getJsVar($recurso, $jsplugin, $var){
        if(!$this->loadJsDoc($recurso, $jsplugin)){
            return "A classe ($jsplugin"."Doc) não existe";
        }
        $obj    = $this->doc[$jsplugin . "Doc"];
        $method = "get$var";
        return $obj->$method();
    }
    
    private function loadJsDoc($recurso, $jsplugin){

        $class = $jsplugin . "Doc";
        $file_name = RESOURCES . $recurso . "/jsplugins/$jsplugin/$class.php";
        if(array_key_exists($class, $this->doc)) return $this->doc[$class];
        
        if(!file_exists($file_name)) return false;
        require_once $file_name;
        
        if(!class_exists($class)) return false;
        $this->doc[$class] = new $class();
        return true;
    }
    
    private function loadDoc($recurso, $subrecurso){

        $class = $subrecurso . "Doc";
        $file_name = RESOURCES . $recurso . "/docs/$class.php";
        if(array_key_exists($class, $this->doc)) return $this->doc[$class];
        
        if(!file_exists($file_name)) return false;
        require_once $file_name;
        
        if(!class_exists($class)) return false;
        $this->doc[$class] = new $class();
        return true;
    }
    
    public function getModelName() {
        return "";
    }
    public function getModelLabel(){
    	return "";
    }
    
    public function getModelDescription(){
    	return "";
    }
    
}

?>
